﻿Imports DTO
Imports System.Data.SqlClient


Public Class DocGia_DAO
    Public Shared Function DocGia_Add(ByVal dg As DocGia_DTO, ByVal tk As TaiKhoan_DTO, ByVal tdg As TheDocGia_DTO) As Boolean
        Dim con As SqlConnection = DataProvider.ConnectToDB
        Try
            Dim cmd As New SqlCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "DocGia_Them"
            cmd.Connection = con

            Dim para As New SqlParameter("tendg", SqlDbType.NVarChar, 30)
            para.Direction = ParameterDirection.Input
            para.Value = dg.tenDG
            cmd.Parameters.Add(para)

            para = New SqlParameter("gioitinh", SqlDbType.Char, 10)
            para.Direction = ParameterDirection.Input
            para.Value = dg.gioiTinh
            cmd.Parameters.Add(para)

            para = New SqlParameter("ngaysinh", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = dg.ngaySinh
            cmd.Parameters.Add(para)

            para = New SqlParameter("email", SqlDbType.Char, 30)
            para.Direction = ParameterDirection.Input
            para.Value = dg.email
            cmd.Parameters.Add(para)

            para = New SqlParameter("dienthoai", SqlDbType.Char, 11)
            para.Direction = ParameterDirection.Input
            para.Value = dg.dienThoai
            cmd.Parameters.Add(para)

            para = New SqlParameter("magttt", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = dg.maGTTT
            cmd.Parameters.Add(para)

            para = New SqlParameter("maloaidg", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = dg.maLoaiDG
            cmd.Parameters.Add(para)

            para = New SqlParameter("madgIdent", SqlDbType.Int)
            para.Direction = ParameterDirection.Output
            cmd.Parameters.Add(para)

            para = New SqlParameter("tentk", SqlDbType.NVarChar, 30)
            para.Direction = ParameterDirection.Input
            para.Value = tk.tenTK
            cmd.Parameters.Add(para)

            para = New SqlParameter("matkhau", SqlDbType.Char, 40)
            para.Direction = ParameterDirection.Input
            para.Value = tk.matKhau
            cmd.Parameters.Add(para)

            para = New SqlParameter("matkIdent", SqlDbType.Int)
            para.Direction = ParameterDirection.Output
            cmd.Parameters.Add(para)

            para = New SqlParameter("ngaydk", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = tdg.ngayDK
            cmd.Parameters.Add(para)

            para = New SqlParameter("ngayhethan", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = tdg.ngayHetHan
            cmd.Parameters.Add(para)

            para = New SqlParameter("matheIdent", SqlDbType.Int)
            para.Direction = ParameterDirection.Output
            cmd.Parameters.Add(para)

            cmd.ExecuteNonQuery()

            dg.maDG = cmd.Parameters("madgIdent").Value


            Dim dest = "images/avartar/" + dg.maDG.ToString + ".jpg"
            System.IO.File.Copy(tdg.hinhAnh, dest)

        Catch ex As Exception
            con.Close()
            Throw New ArgumentException("Đăng ký thất bại")
            Return False
        End Try

        con.Close()
        Return True
    End Function

    Public Shared Function getInfoCardLibrary(ByVal tentk As String) As DataTable
        Dim dt As New DataTable
        Dim con As SqlConnection = DataProvider.ConnectToDB

        Dim cmd As New SqlCommand
        cmd.CommandText = "TheDocGiagetInfo"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = con

        Dim para As New SqlParameter("tentk", SqlDbType.NVarChar, 30)
        para.Direction = ParameterDirection.Input
        para.Value = tentk
        cmd.Parameters.Add(para)

        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(dt)

        con.Close()
        Return dt
    End Function

    'Kiem tra xem email doc gia co trung ko
    Public Shared Function DocGiaKTEmail(ByVal dg As DocGia_DTO) As DataTable
        Dim con As SqlConnection = DataProvider.ConnectToDB
        Dim dt As New DataTable

        Dim cmd As New SqlCommand
        cmd.Connection = con
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DocGiaKiemTraEmail"

        Dim para As New SqlParameter("email", SqlDbType.Char, 30)
        para.Direction = ParameterDirection.Input
        para.Value = dg.email
        cmd.Parameters.Add(para)

        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(dt)
        con.Close()

        Return dt

    End Function

    Public Shared Function DocGiaKTGiayTo(ByVal dg As DocGia_DTO) As DataTable
        Dim con As SqlConnection = DataProvider.ConnectToDB
        Dim dt As New DataTable

        Dim cmd As New SqlCommand
        cmd.Connection = con
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DocGiaKiemTraGiayTo"

        Dim para As New SqlParameter("magttt", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = dg.maGTTT
        cmd.Parameters.Add(para)

        para = New SqlParameter("thongtingttt", SqlDbType.NVarChar, 100)
        para.Direction = ParameterDirection.Input
        para.Value = dg.thongTinGTTT
        cmd.Parameters.Add(para)

        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(dt)
        con.Close()

        Return dt

    End Function


End Class
